package com.hao.syn;

public class SafeBank {
    public static void main(String[] args) {
        // 账户
        Account account = new Account(1000,"结婚基金");
        FetchMoney you = new FetchMoney(account,50,"you");
        FetchMoney girl = new FetchMoney(account,100,"girl");
        you.start();
        girl.start();

    }
}
class Account1 {
    int money;
    String name;

    public Account1(int money, String name) {
        this.money = money;
        this.name = name;
    }
}

// 银行
class FetchMoney1 extends Thread{
    Account1 account;
    int FMoney;
    int HMoney;
    public FetchMoney1(Account1 account, int FMoney, String name) {
        super(name);
        this.account = account;
        this.FMoney = FMoney;
    }
    // 取钱
    @Override
    public  void run() {
        synchronized (account){
            if (account.money-FMoney<0){
                System.out.println(Thread.currentThread().getName()+"钱不够了，不能取！");
                return;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            // 余额
            account.money = account.money-FMoney;
            // 手里的钱
            HMoney = HMoney+FMoney;
            System.out.println(account.name+"余额为："+account.money);
            System.out.println(this.getName()+"手里的钱："+HMoney);
        }

    }
}

